Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming


Encapsulation

You implement a class by combining both state and behavior together in a well-defined unit, which is an implementation of a specified type. The data members (variables, buffers, etc.) represent the object state for a given class. The methods represent the object behavior for a given class, which is what an object of that class can do with its data members or any data that is passed to it. Objects invoke each others’ behavior by sending messages to each other using the interface defined by each object’s type. And an object sends a message to another object by invoking one of the receiving object’s methods, as defined by the object’s interface.

Through the well-defined interface of its class, an object can satisfy its contract to class consumers while keeping details of its implementation private. The general mechanism of thus providing access to class resources to satisfy its contract while maintaining the privacy of its implementation is encapsulation. Because of its goal to maintain the privacy of a class’s implementation, encapsulation is also known as information hiding.

Depending on the degree of encapsulation, a class in object-oriented programming can allow some of its code and data to be accessible from outside the class and some of it to be inaccessible. By enforcing the interface to its accessible state and behavior, you can change the implementation of a class at any time without affecting any other code that uses it.

Thus, classes allow you to define PRIVATE, PROTECTED, and PUBLIC methods. A PRIVATE method allows you to access that method within a class definition. A PROTECTED method allows you to access that method within a class definition and within any class hierarchy that inherits from that class. A PUBLIC method allows you to access that method from within a class definition, from within any class hierarchy that inherits from that class, and from outside that class, within an instance (object) of another class. You can similarly define PRIVATE, PROTECTED, and PUBLIC data members in a class.

Comparison with procedure-based programming

Progress procedures also provide a degree of encapsulation. By default, the variables and other data definitions of a procedure are private and cannot be accessed directly from other procedures. Procedures can use shared variables and other mechanisms to share data between procedures. Alternatively, a procedure can define public internal procedures or user-defined functions to allow controlled access to its data. (Procedures and user-defined functions are public, by default.) Procedures can also define private internal procedures and user-defined functions that cannot be executed from another procedure.

However the class-based mechanism for defining PRIVATE, PROTECTED, and PUBLIC methods and data members allows you to define the degree of encapsulation for object state and behavior with more precision than with procedures.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095